<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="48px">
            <el-form-item label="名称" prop="name">
                <el-input v-model="queryParams.name" placeholder="请输入评分表名称" clearable @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['score:sheet:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['score:sheet:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['score:sheet:export']">导出</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="info" plain icon="el-icon-upload" size="mini" @click="upload.open = true" v-hasPermi="['score:sheet:import']">导入</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="sheetList" @selection-change="handleSelectionChange" :height="table_height">
            <el-table-column type="selection" width="55" align="center"/>
            <el-table-column label="评分表名称" align="center" prop="name" width="250"/>
            <el-table-column label="总分" align="center" prop="total" width="100"/>
            <el-table-column label="题目" align="center" prop="question"/>
            <el-table-column label="备注" align="center" prop="remark"/>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['score:sheet:edit']">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['score:sheet:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

        <!-- 添加或修改评分表管理对话框 -->
        <el-drawer :title="title" :visible.sync="open" size="70%" :destroy-on-close="true" append-to-body>
            <div style="width: 100%;height: 60%">
                <el-tabs v-model="form.model" tab-position="left" :before-leave="tabsHandleClick" :stretch="true">
                    <el-tab-pane label="原始模板" name="model_atom" :lazy="false">
                        <ATOM :form="form" v-if="form.model == 'model_atom'" @submitForm="submitForm"/>
                    </el-tab-pane>
                    <el-tab-pane label="顺序步骤模板" name="model_order_step" :lazy="false">
                        <ORDER_STEP :form="form" v-if="form.model == 'model_order_step'" @submitForm="submitForm" @cancel="cancel"/>
                    </el-tab-pane>
                </el-tabs>
            </div>
        </el-drawer>

        <!-- 题目表导入对话框 -->
        <el-dialog title="导入评分表" :visible.sync="upload.open" width="500px" append-to-body>
            <div style="width: 100%;text-align: center">
                <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers" :action="upload.url"
                           :disabled="upload.isUploading" :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
                    <i class="el-icon-upload"></i>
                    <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
                    <div class="el-upload__tip text-center" slot="tip">
                        <span>仅允许导入xls、xlsx格式文件。</span>
                        <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link><br>
                    </div>
                </el-upload>
            </div>

            <p style="color: #F56C6C;text-align: left;font-size: 12px;">
                1、此模板支持多张sheet<br>
                2、sheet名称会导入为评分表名称<br>
                3、每一个sheet的第一行写<b>题目</b>，如果没有题目请清空该单元格<br>
                4、每一个sheet的第二行写<b>备注</b>，如果没有题目请清空该单元格<br>
                5、每一个sheet的任意放<b>图片</b>，图片将置于单元格上方，置于单元格内将无法读取
                <el-tooltip effect="dark" content="如果是复制进excel文件，则是直接置于单元格上方的，如果是以插入的形式，请选择插入到单元格上方，不能选择插入到单元内部" placement="top">
                    <i class="el-icon-warning-outline"/>
                </el-tooltip>
                <br>
                6、顺序可以为空
            </p>
            <div slot="footer" class="dialog-footer" style="width: 100%;text-align: center">
                <el-button type="primary" @click="submitFileForm">确 定</el-button>
                <el-button @click="upload.open = false">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {listSheet, delSheet, addSheet, updateSheet, getSheet} from "@/api/score/sheet";
import ATOM from './models/atom.vue';
import ORDER_STEP from './models/order_step.vue';
import {isNotNull} from "@/utils/zzDouUtils/objUtil";
import {getToken} from "@/utils/auth";
import {nowDateStr} from "@/utils/zzDouUtils/dateUtil";

export default {
    name: "Sheet",
    components: { ATOM , ORDER_STEP},
    data() {
        return {
            skillId: null,
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 评分表管理表格数据
            sheetList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                name: null
            },
            // 表单参数
            form: {
                model: 'model_atom',
                name: 'XX评分表',
                question: null,
                stepName: '步骤',
                isSplitScore: '2',
                data:[],
                total: null,
                remark: null,
                editOpen: false,
            },
            // 表单校验
            rules: {},
            // 导入参数
            upload: {
                // 是否显示弹出层
                open: false,
                // 是否禁用上传
                isUploading: false,
                // 设置上传的请求头部
                headers: {Authorization: "Bearer " + getToken()},
                // 上传的地址
                url: null
            },
        };
    },
    created() {
        this.skillId = this.$route.params.skillId;
        this.upload.url = process.env.VUE_APP_BASE_API + "/score/sheet/import/" + this.skillId;
        this.getList();
    },
    methods: {
        /** 查询评分表管理列表 */
        getList() {
            this.queryParams.skillId = this.skillId;
            this.loading = true;
            listSheet(this.queryParams).then(response => {
                this.sheetList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                model:'model_atom',
                skillId: this.skillId,
                name: 'XX评分表',
                question: null,
                stepName: '步骤',
                isSplitScore: '2',
                steps:[],
                total: null,
                remark: null,
                editOpen: false,
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加评分表";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids
            getSheet(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改"+response.data.name;
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.form.skillId = this.skillId;
            if (this.form.id != null) {
                updateSheet(this.form).then(response => {
                    this.$modal.msgSuccess("修改成功");
                    this.open = false;
                    this.getList();
                });
            } else {
                addSheet(this.form).then(response => {
                    this.$modal.msgSuccess("新增成功");
                    this.open = false;
                    this.getList();
                });
            }
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm(`是否确认删除评分表管理编号为 ${ids} 的数据项？`).then(function () {
                return delSheet(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => {
            });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('score/sheet/export', {...this.queryParams}, `评分表_${nowDateStr()}.xlsx`)
        },
        /** 导出模版 */
        importTemplate() {
            this.download('score/sheet/importTemplate', {}, `评分表模板_${nowDateStr()}.xlsx`)
        },
        // 文件上传中处理
        handleFileUploadProgress(event, file, fileList) {
            this.upload.isUploading = true;
        },
        // 文件上传成功处理
        handleFileSuccess(response, file, fileList) {
            this.upload.open = false;
            this.upload.isUploading = false;
            this.$refs.upload.clearFiles();
            this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", {dangerouslyUseHTMLString: true});
            this.getList();
        },
        // 提交上传文件
        submitFileForm() {
            this.$refs.upload.submit();
        },
        /** 标签页点击事件 */
        tabsHandleClick(activeName, oldActiveName){
            if (isNotNull(this.form.id) && this.form.model != activeName) {
                return false;
            }else{
                this.reset();
                return true;
            }
        },
    }
};
</script>
